package com.showcai.controller.drawGame;

import com.showcai.controller.base.WeChatBaseController;
import com.showcai.module.Prizes;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.redis.RedisCache;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.DrawGameService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "drawGame")
public class DrawGameController extends WeChatBaseController {

    @Autowired
    RedisCache redisCache;
    @Autowired
    DrawGameService chouqianService;

    private final static Integer EXPIRE = 60 * 60 * 24;

    @RequestMapping(value = "/drawGame")
    public ModelAndView draw() {
        ModelAndView mov = new ModelAndView();
        String viewe = "drawGame/web/drawGame";
        if (getIsMobile()) {

            logger.info("微信浏览器要鉴权！！！");
            super.oauth2();
            viewe = "drawGame/wap/drawGame";
        }

        mov.setViewName(viewe);
        return mov;
    }


    @RequestMapping(value = "/actionDraw", method = RequestMethod.POST)
    @ResponseBody
    public String actionDraw() {
        logger.info("abc===================================super.getOpenId()===========================================>>>>>>>>>" + super.getOpenId());
        ResultObj resultObj = new ResultObj();
        try {
            logger.info("session is==============>>>"+super.session.getId());

            String openIdInSession = super.getOpenId();
            String openId = (String) redisCache.get(openIdInSession);
            String abc= (String)super.session.getAttribute("abc");
            logger.info("abc==============================================================================>>>>>>>>>" + abc);
            logger.info("openId==============================================================================>>>>>>>>>" + openId);
            logger.info("openIdInSession==============================================================================>>>>>>>>>" + openIdInSession);

            if (StringUtils.isNotBlank(openId)) {
                throw new MyRunTimeExcption("你已经抽过了");
            }
            Prizes prizes = chouqianService.actionDraw(null, null);
            resultObj.setData(prizes);
            if (StringUtils.isNotBlank(openIdInSession)) {
                logger.info("openIdInSession is =======写入redies======>>>>"+openIdInSession);
                redisCache.put(openIdInSession, openIdInSession, EXPIRE);
            }

        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj.toJson();
    }


}

